﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using Dapper;
using TaxBusiness.Model;

namespace TaxBusiness.DAL
{
    public class BillUnitDB
    {
        private string _insert = "INSERT INTO [BillUnit] " +
                                 "([Name])" +
                                 " VALUES " +
                                 "(@Name)";
        private string _delete = "DELETE FROM [BillUnit] WHERE ID=@ID";

        private string _select = "SELECT * FROM [BillUnit] order by id";

        private string _selectByID = "SELECT * FROM [BillUnit] WHERE ID=@ID";

        public int Insert(Model.TheOffice billUnit)
        {
            using (var cn = new SqlConnection(SqlConnectString.CONNSTRING))
            {
                return cn.Execute(_insert, billUnit);
            }
        }

        public int Delete(int id)
        {
            using (var cn = new SqlConnection(SqlConnectString.CONNSTRING))
            {
                return cn.Execute(_delete, new { ID = id });
            }
        }

        public List<Model.TheOffice> GetList()
        {
            using (var cn = new SqlConnection(SqlConnectString.CONNSTRING))
            {
                return cn.Query<Model.TheOffice>(_select).ToList();
            }
        }

        public string GetName(int id)
        {
            Model.TheOffice billUnit = new TheOffice();
            using (var cn = new SqlConnection(SqlConnectString.CONNSTRING))
            {
                billUnit = cn.Query<Model.TheOffice>(_selectByID, new { ID = id }).First();
            }
            return billUnit.Name;
        }
    }
}
